package com.study.rabbitmq.util;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/**
 * 连接工厂创建信道的工具类
 * @Author chenyeqing
 * @Date 2021/9/2 21:50
 */
public class RabbitUtil {

    public static final String QUEUE_NAME = "hello";

    public static Channel getChannel() {
        // 创建一个连接工厂
        ConnectionFactory factory = new ConnectionFactory();

        // 工厂 IP 连接 RabbitMQ 的队列
        factory.setHost("localhost");
        // 用户名
        factory.setUsername("admin");
        // 密码
        factory.setPassword("123");
        // 创建连接
        Connection connection = null;
        try {
            connection = factory.newConnection();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (TimeoutException e) {
            e.printStackTrace();
        }
        // 获取信道
        try {
            return connection.createChannel();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
}
